<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/20
 * Time: 14:38
 */
namespace App\Http\Controllers\Login;

use App\Http\Controllers\WebController;
use App\Model\UserInfoModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Log;

class LoginController extends WebController
{
    public function index()
    {
        return view('login.index');
    }

    public function create(Request $request)
    {
        $data = $request->all();
        $request->flash();
        if($data['password']!=$data['cpassword']){
            return back()->with('success', '请把密码输入一致!');
        }
        $userId = UserInfoModel::where('name', $data['name'])->first();
        if($userId){
            return back()->with('success', '用户名已经存在!');
        }
        $data['password'] = md5($data['password']);
        $id = UserInfoModel::create($data);
        if ($id) {
            session(['userid' => $id['id']]);
            back()->with('success', '注册成功！');
            return redirect('/login');
        } else {
            return back()->with('success', '注册失败!');
        }
    }

    public function zuce(Request $request)
    {
        $name = $request->get('name');
        $userId = UserInfoModel::where('name', $name)->first();
        if ($userId) {
            return response()->json(array(
                'status' => 'false',
                'val' => '用户名已经存在！',
            ));
        }else{
            return response()->json(array(
                'status' => 'true',
                'val' => $name,
            ));
        }
    }

    public function show()
    {
        return view('login.zuce');
    }

    public function store(Request $request)
    {
        $userInfo = $request->all();
        $request->flash();
        if (strnatcasecmp($userInfo['captcha'], session('milkcaptcha'))) {
            return back()->with('success', '验证码错误！');
        }
        $id = UserInfoModel::where('name', $userInfo['name'])->where('password', md5($userInfo['password']))->first();
        session(['userid' => $id['user_id']]);
        if ($id) {
            back()->with('success', '登录成功！');
            return redirect('/');
        } else {
            return back()->with('success', '登录失败!请检查用户名或密码！');
        }
    }
}